﻿<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${info.mapperPackage}.${info.mapperName}">
    <resultMap id="BaseResultMap" type="${info.entityPackage}.${info.entityName}">
    <#list tableList as table>
    <#if table.primary>
        <id column="${table.colname}" jdbcType="${table.jdbcType}" property="${table.lowerColname}" />
    <#else>
        <result column="${table.colname}" jdbcType="${table.jdbcType}" property="${table.lowerColname}" />
    </#if>
    </#list>
    </resultMap>
    <sql id="Base_Column_List">
    <#list tableList as table>${table.colname}<#if table_has_next>,</#if></#list>
    </sql>

    <#if tableKey?exists>
    <select id="getById" parameterType="java.lang.${tableKey.javaType}" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ${tableName}
        where ${tableKey.colname} = ${r'#{'}${tableKey.lowerColname},jdbcType=${tableKey.jdbcType}${r'}'}
    </select>
    <delete id="deleteById" parameterType="java.lang.${tableKey.javaType}">
        delete from ${tableName}
        where ${tableKey.colname} = ${r'#{'}${tableKey.lowerColname},jdbcType=${tableKey.jdbcType}${r'}'}
    </delete>
    </#if>

    <insert id="insert" parameterType="${info.entityPackage}.${info.entityName}">
        <#if needSelectKey?? && needSelectKey>
        <#if tableKey.javaType == 'String'>
        <selectKey keyProperty="${tableKey.lowerColname}" order="BEFORE" resultType="java.lang.${tableKey.javaType}">
            select replace(uuid(),'-','')
        </selectKey>
        <#else>
        <selectKey keyProperty="${tableKey.lowerColname}" order="AFTER" resultType="java.lang.${tableKey.javaType}">
            SELECT LAST_INSERT_ID()
        </selectKey>
        </#if>
        </#if>
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="${tableKey.lowerColname} != null">
            ${tableKey.colname},
            </if>
            <#list tableList as table>
            <#if !table.primary>
            <if test="${table.lowerColname} != null">
                ${table.colname},
            </if>
            </#if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="${tableKey.lowerColname} != null">
            ${r'#{'}${tableKey.lowerColname},jdbcType=${tableKey.jdbcType}${r'}'},
            </if>
            <#list tableList as table>
            <#if !table.primary>
            <if test="${table.lowerColname} != null">
            ${r'#{'}${table.lowerColname},jdbcType=${table.jdbcType}${r'}'},
            </if>
             </#if>
            </#list>
        </trim>
    </insert>

<#if tableKey?exists>
    <update id="updateById" parameterType="${info.entityPackage}.${info.entityName}">
        <!--
          WARNING - @mbggenerated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        update ${tableName}
        <set>
        <#list tableList as table>
        <#if !table.primary>
                <if test="${table.lowerColname} != null">
                ${table.colname} = ${r'#{'}${table.lowerColname},jdbcType=${table.jdbcType}${r'}'},
                </if>
        </#if>
        </#list>
        </set>
        where ${tableKey.colname} = ${r'#{'}${tableKey.lowerColname},jdbcType=${tableKey.jdbcType}${r'}'}
    </update>
</#if>
</mapper>